package cn.shyouchi.wuguanjia.android.ui.event.arrange;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.ui.event.labor.LaborRequestEntity;

/**
 * Created by Eenie on 2016/11/25 at 16:01
 * Email: 472279981@qq.com
 * Des:
 */

public class ArrangeDateAdapter extends BaseQuickAdapter<LaborRequestEntity.ResultMessageBean, BaseViewHolder> {

    EventArrangeContract.View mView;
    int selPosition = 0;

    public ArrangeDateAdapter(List<LaborRequestEntity.ResultMessageBean> data, EventArrangeContract.View view) {

        super(R.layout.item_event_arrange_date_layout, data);
        this.mView = view;
//        String date = new SimpleDateFormat("").format(System.currentTimeMillis());
//        int p = 0;
//        for (LaborRequestEntity.ResultMessageBean b : data) {
//            p++;
//            LogUtil.e(b.getDate());
//            if (b.getDate().equals(date)) {
//                selPosition = p;
//                return;
//            }
//        }


    }

    @Override
    protected void convert(final BaseViewHolder holder, final LaborRequestEntity.ResultMessageBean entity) {
        holder.setText(R.id.tv_date, String.format("%1s  %2s", new SimpleDateFormat("MM-dd").format(covertTime(entity.getDate()).getTime()), covertWeek(entity.getDate())));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.getAdapterPosition() != selPosition) {
                    if (mView.isSave()) {
                        selPosition = holder.getAdapterPosition();
                        notifyDataSetChanged();
                    } else {
                        mView.showAlert();
                    }
                }
            }
        });

        TextView t = holder.getView(R.id.tv_date);
        if (holder.getAdapterPosition() == selPosition) {


            t.setBackgroundColor(mContext.getResources().getColor(R.color.mainColor));


            t.setTextColor(mContext.getResources().getColor(R.color.color_md_white));


            mView.onItemClick(entity);
        } else {
            t.setBackgroundColor(mContext.getResources().getColor(R.color.color_md_white));
            t.setTextColor(Color.parseColor("#000000"));
            holder.getView(R.id.tv_date).setSelected(false);
        }
    }

    private Calendar covertTime(String time) {
        Calendar dayc1 = new GregorianCalendar();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date daystart = null;
        try {
            daystart = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        dayc1.setTime(daystart);
        return dayc1;
    }


    private String covertWeek(String date) {

        String week = "";
        switch (covertTime(date).get(Calendar.DAY_OF_WEEK)) {
            case 1:
                week = "周日";
                break;
            case 2:
                week = "周一";
                break;
            case 3:
                week = "周二";
                break;
            case 4:
                week = "周三";
                break;
            case 5:
                week = "周四";
                break;
            case 6:
                week = "周五";
                break;
            case 7:
                week = "周六";
                break;

            default:
                week = "未知";
                break;
        }
        return week;

    }


}
